package com.cloudptt.api.product.func.sys.fsm;

import android.content.Context;

import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.func.call.group.fsm.IRollback;
import com.cloudptt.api.product.func.call.group.fsm.RollbackTimer;
import com.cloudptt.api.product.func.sys.SysFunc;
import com.cloudptt.api.product.func.sys.SysStateInfo;
import com.cloudptt.api.product.log.Log;

public class SipPublishingState implements ISysState, IRollback {

	private SysAgent sysAgent = null;
	private RollbackTimer rollbackTimer = null;
	private boolean isRefresh = false;
	public SipPublishingState(Context ctx, SysAgent sysAgent)
	{
		this.sysAgent = sysAgent;
		this.rollbackTimer = new RollbackTimer(ctx,10,this);
	}
	@Override
	public int doSysAction(int sysAction) {
		// TODO Auto-generated method stub
		if(SysConst.SysAction_Logout == sysAction)
		{
			this.sysAgent.doSipUnAuth();
			this.sysAgent.goToState(SysStateInfo.SysState_SvrStartState,SysStateInfo.Cause_Logout);
		}
		return 0;
	}

	@Override
	public void recevSysMsg(int sysMsg) {
		// TODO Auto-generated method stub
		Log.d(SysFunc.Log_Key, "SipAuthingState recevSysMsg sysMsg = " + sysMsg);
		if(SysConst.SysMsg_Svr_Stop == sysMsg)
		{
			this.sysAgent.goToState(SysStateInfo.SysState_SvrStopState,SysStateInfo.Cause_Suc);
		}
//		else if(sysMsg == SysConst.SysMsg_Sip_Auth_Suc)
//		{
//			sysAgent.goToState(SysStateInfo.SysState_SipAuthSucState,SysStateInfo.Cause_Suc);
//		}
		else if(sysMsg == SysConst.SysMsg_Sip_Auth_Failed)
		{
			sysAgent.goToState(SysStateInfo.SysState_SvrStartState,SysStateInfo.Cause_Failed);
		}
		else if(sysMsg == SysConst.SysMsg_Sip_Auth_Timeout)
		{
			sysAgent.goToState(SysStateInfo.SysState_SvrStartState,SysStateInfo.Cause_Timeout);
		}
		else if(SysConst.SysMsg_Logout_Suc == sysMsg)
		{
			this.sysAgent.goToState(SysStateInfo.SysState_SvrStartState,SysStateInfo.Cause_Logout);
		}
	}
	@Override
	public void recevHttpResult(boolean result,int code) {
		// TODO Auto-generated method stub
	}
	@Override
	public int recevPublishResult(int mcType,int publishCount,boolean isSuc)
	{
		Log.d(SysFunc.Log_Key,"recevPublishResult mcType = " +mcType +  " publishCount = " +publishCount +  " isSuc = " +isSuc);
		if(isSuc)
		{
			sysAgent.goToState(SysStateInfo.SysState_SipPulishSucState, SysStateInfo.Cause_Suc);
		}
		else
		{
			if(publishCount == CallAttr.McType_All) {
				this.sysAgent.doSipUnAuth();
				sysAgent.goToState(SysStateInfo.SysState_SvrStartState, SysStateInfo.Cause_Publish_None);
			}
		}
		return 0;
	}

	@Override
	public int getState() {
		// TODO Auto-generated method stub
		return SysStateInfo.SysState_SipPulishingState;
	}

	@Override
	public void destory() {
		// TODO Auto-generated method stub
		if(rollbackTimer != null)
		{
			rollbackTimer.cancelTimer();
			rollbackTimer = null;
		}
		sysAgent = null;
	}
	@Override
	public void rollback() {
		// TODO Auto-generated method stub
		Log.d(SysFunc.Log_Key, SysStateInfo.stateTag(this.getState()) + " rollback");
		this.sysAgent.doSipUnAuth();
		this.sysAgent.goToState(SysStateInfo.SysState_SvrStartState,SysStateInfo.Cause_Publish_None);
	}

}
